package com.ayoka.parenting.ui.grown.index;

import com.ayoka.parenting.Constants;
import com.ayoka.parenting.R;
import com.ayoka.parenting.adapter.GrownIndexClassroomAdapter;
import com.ayoka.parenting.bean.ClassroomBean;
import com.ayoka.parenting.bean.GrownIndexBean;
import com.ayoka.parenting.databinding.FooterGrownIndexBinding;
import com.ayoka.parenting.databinding.FragmentBaseRecyclerviewBinding;
import com.ayoka.parenting.databinding.HeaderClassroomScheduleBinding;
import com.ayoka.parenting.databinding.LayoutGrownEmptyViewBinding;
import com.ayoka.parenting.mvp.MVPBaseRecyclerViewFragment;
import com.ayoka.parenting.ui.grown.detail.GrownDetailActivity;
import com.ayoka.parenting.ui.live.liveclassroom.LiveClassroomActivity;
import com.ayoka.parenting.ui.grown.list.GrownListActivity;
import com.ayoka.parenting.widget.MyCountDownTextView;
import com.ayoka.parenting.widget.MyCountDownTimer;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.utils.DateUtil;
import com.jincp.common.utils.LiveDataBus;
import com.jincp.common.utils.SharedPreferenceUtil;
import com.jincp.common.utils.StringUtil;
import com.jincp.image.load.ImageLoader;
import com.jincp.image.load.ImageLoaderOptions;

import android.arch.lifecycle.Observer;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import java.util.ArrayList;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: GrownIndexFragment.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class GrownIndexFragment extends MVPBaseRecyclerViewFragment<ClassroomBean, FragmentBaseRecyclerviewBinding, GrownIndexContract.View, GrownIndexPresenter> implements GrownIndexContract.View {

    private LayoutGrownEmptyViewBinding emptyViewBinding;
    private HeaderClassroomScheduleBinding headerView;
    private FooterGrownIndexBinding footerView;

    private GrownIndexBean grownIndexBean;

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_base_recyclerview;
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);

    }

    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return mDataBinding.swipeRefresh;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        return mDataBinding.recyclerView;
    }

    @NonNull
    @Override
    protected BaseQuickAdapter<ClassroomBean, BaseViewHolder> getAdapter() {
        GrownIndexClassroomAdapter adapter = new GrownIndexClassroomAdapter();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ClassroomBean classroomBean = (ClassroomBean) adapter.getItem(position);
                GrownDetailActivity.start(getActivity(), classroomBean.ID);
            }
        });
        return adapter;
    }

    @Override
    protected RecyclerView.ItemDecoration getRecycleDivider() {
        return null;
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {
        if (mPresenter != null) {
            mPresenter.getMyClassRoomList();
        }
    }

    @Override
    protected void initData() {
        LiveDataBus.getInstance().with("GrownFragment", String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String value) {
                if (TextUtils.equals("Notify", value) && mPresenter != null) {
                    onRefresh();
                }
            }
        });
        getData(true);
    }

    @Override
    protected GrownIndexPresenter getPresenter() {
        return new GrownIndexPresenter();
    }

    @Override
    public void onClick(View view) {
        if (emptyViewBinding != null && view == emptyViewBinding.btnJoinGrown) {
            GrownListActivity.start(getActivity());
        } else if (footerView != null && view == footerView.tvJoinTitle) {
            GrownListActivity.start(getActivity());
        } else if (headerView != null && view == headerView.btnJoinClassroom) {
            if (grownIndexBean != null && grownIndexBean.nearestSchedule != null && mPresenter != null) {
                mPresenter.createLiveRoom(grownIndexBean.nearestSchedule.RoomID, grownIndexBean.nearestSchedule.ID);
                headerView.btnJoinClassroom.setEnabled(false);
            }
        }
    }

    @Override
    public void configMyClassRoomList(GrownIndexBean data) {
        if (data == null) {
            setLoadMoreLayout(new ArrayList<ClassroomBean>());
            return;
        }
        this.grownIndexBean = data;
        // 设置数据
        setLoadMoreLayout(data.classRoomList);
        // 设置背景色
        if ((grownIndexBean.classRoomList == null || grownIndexBean.classRoomList.isEmpty()) && grownIndexBean.nearestSchedule == null) {
            mDataBinding.recyclerView.setBackgroundResource(R.color.white);
        } else {
            mDataBinding.recyclerView.setBackgroundResource(R.color.colorPrimary);
        }
        // 设置头部
        if (data.nearestSchedule == null) {
            if (headerView != null) {
                headerView.layoutNearSchedule.setVisibility(View.GONE);
                headerView.tvTestCourse.setVisibility(View.GONE);
                headerView.tvCourseName.setVisibility(View.GONE);
            }
        } else {
            if (headerView == null) {
                headerView = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.header_classroom_schedule, null, false);
                adapter.addHeaderView(headerView.getRoot());
                headerView.btnJoinClassroom.setOnClickListener(this);
            }
            headerView.layoutNearSchedule.setVisibility(View.VISIBLE);
            configNearSchedule();
            MyCountDownTimer.getInstance().start();
        }
        // 设置底部
        if (grownIndexBean.classRoomList != null && !grownIndexBean.classRoomList.isEmpty()) {
            if (footerView == null) {
                footerView = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.footer_grown_index, null, false);
                adapter.addFooterView(footerView.getRoot());
                footerView.tvJoinTitle.setOnClickListener(this);
            }
        }
    }

    @Override
    public void onCreateLiveRoomCallback(boolean isSuccess, int roomId, String scheduleId) {
        if (isSuccess) {
            SharedPreferenceUtil.putInt("RoomId", roomId);
            SharedPreferenceUtil.putString("ScheduleId", scheduleId);
            LiveClassroomActivity.start(getActivity());
        }
        headerView.btnJoinClassroom.setEnabled(true);
    }

    private void configNearSchedule() {
        if (grownIndexBean.nearestSchedule != null) {
            if (grownIndexBean.nearestSchedule.ClassRoom != null) {
                headerView.tvTestCourse.setVisibility(grownIndexBean.nearestSchedule.ClassRoom.IsOnTrial == Constants.ClassroomType.TEST ? View.VISIBLE : View.GONE);
                headerView.tvGrownCourse.setVisibility(grownIndexBean.nearestSchedule.ClassRoom.IsOnTrial == Constants.ClassroomType.GROW ? View.VISIBLE : View.GONE);
                if (grownIndexBean.nearestSchedule.ClassRoom.Coach != null) {
                    ImageLoader.getInstance().displayImage(grownIndexBean.nearestSchedule.ClassRoom.Coach.LocalAvatar, headerView.ivCoachHeader, ImageLoaderOptions.getCircleOptions());
                    headerView.tvCoachName.setText(grownIndexBean.nearestSchedule.ClassRoom.Coach.WeixinNickName);
                }
                headerView.tvClassroomName.setText(grownIndexBean.nearestSchedule.ClassRoom.Title);
            }
            headerView.tvCourseName.setText(StringUtil.formatString(R.string.format_course_name, grownIndexBean.nearestSchedule.CourseTitle));
            headerView.tvClassroomTime.setText(StringUtil.formatString(R.string.format_classroom_time, grownIndexBean.nearestSchedule.StartTime));
            long endMillSecond = DateUtil.parseDate(grownIndexBean.nearestSchedule.StartTime).getTime();
            headerView.tvCountDown.setEndMilliSeconds("倒计时：%s", endMillSecond, new MyCountDownTextView.CountDownListener() {
                @Override
                public void onTick(long endMillSecond) {
                    if (endMillSecond > 1800 * 1000){
                        headerView.btnJoinClassroom.setEnabled(false);
                    } else {
                        headerView.btnJoinClassroom.setEnabled(true);
                    }
                }
            });
        }
    }

    @Override
    protected View getEmptyView() {
        if (emptyViewBinding == null) {
            emptyViewBinding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.layout_grown_empty_view, null, false);
        }
        emptyViewBinding.btnJoinGrown.setOnClickListener(this);
        return emptyViewBinding.getRoot();
    }

    @Override
    public void onDestroy() {
        MyCountDownTimer.getInstance().stop();
        super.onDestroy();
    }
}